package com.kitwee.serialport.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public final class Times {

    public static SimpleDateFormat DEFAULT_SDF;

    static {
        DEFAULT_SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    }

    private Times() {
        throw new UnsupportedOperationException("hey, buddy, easy...");
    }

    public static String currentDateString() {
        return currentDateString(DEFAULT_SDF);
    }

    public static String currentDateString(SimpleDateFormat format) {
        return date2String(new Date(), format);
    }

    public static long currentTimestamp() {
        return System.currentTimeMillis();
    }

    public static Date parseDateString(String dateString) {
        return parseDateString(dateString, DEFAULT_SDF);
    }

    public static Date parseDateString(String dateString, SimpleDateFormat format) {
        try {
            return format.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new Date();
    }

    public static String formatTimestamp(long timestamp) {
        return formatTimestamp(timestamp, DEFAULT_SDF);
    }

    public static String formatTimestamp(long timestamp, SimpleDateFormat format) {
        return date2String(new Date(timestamp), format);
    }

    public static String date2String(Date time, SimpleDateFormat format) {
        return format.format(time);
    }


}
